/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vce.election;

import com.vce.*;
import com.vce.SecurityManager;
import com.vce.election.domain.*;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 *
 * @author Ggutierrez
 */
public class Election {

    private ElectionDao dao;
    private SecurityManager securityManager;

    public Election() {
    }

    public void setDao(ElectionDao dao) {
        this.dao = dao;
    }

    public void setSecurityManager(SecurityManager securityManager) {
        this.securityManager = securityManager;
    }

    public void attach(Object object) {
        dao.attach(object);
    }

    public User authenticate(String login, String password) {
        User user = dao.findUserByLogin(login);
        if (user == null) {
            //TODO: throw exception, example UserNotFoundException
            return null;
        }

        if (!user.checkPassword(password)) {
            //TODO: throw exception, example BadCredentialException
            return null;
        }
        return user;
    }

    public Year getYear(Long id) {
        return dao.getYear(id);
    }

    public User getUser(Long id) {
        return dao.getUser(id);
    }

    public User getCurrentUser() {
        return securityManager.getCurrentUser();
    }

    public ElectionType getElectionType(Long id) {
        return dao.getElectionType(id);
    }

    public ElectionType getCurrentElectionType(boolean current) {
        return dao.getElectionType(current);
    }

    public Party getParty(Long id) {
        return dao.getParty(id);
    }

    public Candidate getCandidate(Long id) {
        return dao.getCandidate(id);
    }

    public Position getPosition(Long id) {
        return dao.getPosition(id);
    }

    public State getState(Long id) {
        return dao.getState(id);
    }

    public Town getTown(Long id) {
        return dao.getTown(id);
    }

    public BallotCentre getBallotCentre(Long id) {
        return dao.getBallotCentre(id);
    }

    public Jrv getJrv(Long id) {
        return dao.getJrv(id);
    }

    public Document getDocument(Long id) {
        return dao.getDocument(id);
    }

    public Vote getVote(Long id) {
        return dao.getVote(id);
    }

    public Fiscal getFiscal(Long id) {
        return dao.getFiscal(id);
    }

    public Impugnment getImpugnement(Long id) {
        return dao.getImpugnement(id);
    }

    public List<Year> findYears() {
        return dao.findYears();
    }

    public List<User> findUsers() {
        return dao.findUsers();
    }

    public User findUserByLogin(String login) {
        return dao.findUserByLogin(login);
    }

    public List<ElectionType> findElectionTypes() {
        return dao.findElectionTypes();
    }

    public List<ElectionType> findElectionTypes(Year year) {
        return dao.findElectionTypes(year);
    }

    public List<Party> findParties() {
        return dao.findParties();
    }

    public List<Party> findParties(ElectionType electionType) {
        return dao.findParties(electionType);
    }

    public List<Candidate> findCandidates() {
        return dao.findCandidates();
    }

    public List<Candidate> findCandidates(Party party) {
        return dao.findCandidates(party);
    }

    public List<Candidate> findCandidates(Year year, Party party) {
        return dao.findCandidates(year, party);
    }

    public List<Position> findPositions() {
        return dao.findPositions();
    }

    public List<State> findStates() {
        return dao.findStates();
    }
    
    public List<State> findStates(String name) {
        return dao.findStates(name);
    }
    
    public State findState(String name){
        return dao.findState(name);
    }

    public List<Town> findTowns() {
        return dao.findTowns();
    }

    public List<Town> findTowns(State state) {
        return dao.findTowns(state);
    }
    
    public List<Town> findTowns(String name, State state) {
        return dao.findTowns(name, state);
    }
    
    public List<Town> findTowns(String name) {
        return dao.findTowns(name);
    }

    public List<BallotCentre> findBallotCentres() {
        return dao.findBallotCentres();
    }

    public List<BallotCentre> findBallotCentres(Town town) {
        return dao.findBallotCentres(town);
    }
    
    public List<BallotCentre> findBallotCentres(String name, Town town) {
        return dao.findBallotCentres(name, town);
    }
    
    public List<BallotCentre> findBallotCentres(String name) {
        return dao.findBallotCentres(name);
    }

    public List<Jrv> findJrvs() {
        return dao.findJrvs();
    }

    public List<Jrv> findJrvs(BallotCentre ballotCentre) {
        return dao.findJrvs(ballotCentre);
    }
    
    public List<Jrv> findJrvs(String code, BallotCentre ballot) {
        return dao.findJrvs(code, ballot);
    }
    
    public List<Jrv> findJrvs(String code) {
        return dao.findJrvs(code);
    }

    public List<Jrv> findJrvs(String code, User user, Counting counting, Round round) {
        return dao.findJrvs(code, user, counting, round);
    }
    
    public List<Jrv> findInsertedJrvs(String code, User user, Counting counting, Round round) {
        return dao.findInsertedJrvs(code, user, counting, round);
    }
    
    public List<Jrv> findJrvs(String code, BallotCentre ballot, Counting counting, Round round) {
        return dao.findJrvs(code, ballot, counting, round);
    }
    
    public List<Jrv> findRegisteredJrvs(String code, BallotCentre ballot, Counting counting, Round round) {
        return dao.findRegisteredJrvs(code, ballot, counting, round);
    }
    
    public List<Document> findDocuments() {
        return dao.findDocuments();
    }

    public List<Vote> findVotes() {
        return dao.findVotes();
    }

    public List<Vote> findVotes(Document document) {
        return dao.findVotes(document);
    }

    public TreeMap<Party, Long> findElectionStalls(ElectionType electionType) {
        TreeMap<Party, Long> stalls = new TreeMap<Party, Long>(new MyComparator());
        // Find Parties
        List<Party> parties = findParties(electionType);
        for (Party party : parties) {
            stalls.put(party, 0L);
        }

        return stalls;
    }
    
    public void storeYear(Year year){
        dao.storeYear(year);
    }
    
    public void storeUser(User user){
        dao.storeUser(user);
    }
    
    public void storeElectionType(ElectionType electionType){
        dao.storeElectionType(electionType);
    }
    
    public void storeCounting(Town town){
        dao.storeTown(town);
    }
    
    public void storeParty(Party party){
        dao.storeParty(party);
    }
    
    public void storeCandidate(Candidate candidate){
        dao.storeCandidate(candidate);
    }
    
    public void storePosition(Position position){
        dao.storePosition(position);
    }
    
    public void storeState(State state){
        dao.storeState(state);
    }
    
    public void storeTown(Town town){
        dao.storeTown(town);
    }
    
    public void storeBallotCentre(BallotCentre ballotCentre){
        dao.storeBallotCentre(ballotCentre);
    }
    
    public void storeJrv(Jrv jrv){
        dao.storeJrv(jrv);
    }
    
    public void storeDocument(Document document){
        dao.storeDocument(document);
    }
    
    public void storeVote(Vote vote){
        dao.storeVote(vote);
    }
    
    public void storeFiscal(Fiscal fiscal){
        dao.storeFiscal(fiscal);
    }
    
    public void storeImpugnment(Impugnment impugnment){
        dao.storeImpugnment(impugnment);
    }

    static class MyComparator implements Comparator {

        public int compare(Object o1, Object o2) {
            Party i1 = (Party) o1;
            Party i2 = (Party) o2;
            
            Long val1 = new Long((long)i1.getStall());
            Long val2 = new Long((long)i2.getStall());
            
            return val1.compareTo(val2);
        }
    }
    
    public Document getlastInsertedDocument(User user, Counting counting){
        return dao.getlastInsertedDocument(user, counting);
    }
    
    public int getTotalInsertedDocuments(User user, Counting counting){
        return dao.getTotalInsertedDocuments(user, counting);
    }
    
    public Document getDocument(Jrv jrv, Counting counting, Round round){
        return dao.getDocument(jrv, counting, round);
    }
    
    public List<Padron> findElectors(String cedula){
        return dao.findElectors(cedula);
    }
    
    public List<Object[]> findResults(Jrv jrv, Counting counting) throws Exception{
        return dao.findResults(jrv, counting);
    }
}
